﻿using System;
using System.Linq;
using System.Web.Mvc;
using Solution.Business;
using Solution.Data;
using Solution.Utilities;
using Solution.Utilities.DynamicQueryable;
using Solution.Business.Infrastructure;

namespace Solution.Mvc.Areas.Admin.Controllers
{
    [Authorize]
    public class SysMenuCPController : Controller
    {
        MenuBLL Repository = new MenuBLL();
        [CAuthorize(Module = "system", Functions = "V")]
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetData(bool? showBin, string sidx, string sord, int page, int rows)
        {
            bool _showBin = showBin ?? false;
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = Repository.GetAll().Where(t => t.flag != "D").Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
            var questions = Repository.GetAll().Where(t => t.flag != "D").OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).ToList();
            if (_showBin)
            {

                totalRecords = Repository.GetAll().Where(t => t.flag == "D").Count();
                totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
                questions = Repository.GetAll().Where(t => t.flag == "D").OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).ToList();
            }
            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (from question in questions
                        select new
                        {
                            i = question.Id.ToString(),
                            cell = new string[] {
                                question.Id.ToString(), 
                                question.PId.ToString(), 
                                question.Name.ToString(),
                                question.DisplayName,
                                question.Module.Name,
                                question.Link,
                                question.flag.ToString() }
                        }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        [CAuthorize(Module = "system", Functions = "I,D,U")]
        public JsonResult Edit(string oper, bool? delForever, string[] id, string flag, FormCollection collection)
        {
            string[] ids = id;
            bool _delForever = delForever ?? false;
            int Pid = ConvertUtility.ToInt(collection["Pid"]);
            string Name = ConvertUtility.ToString(collection["Name"]);
            string DisplayName = ConvertUtility.ToString(collection["DisplayName"]);
            string MName = ConvertUtility.ToString(collection["MName"]);
            string Link = ConvertUtility.ToString(collection["Link"]);

            switch (oper)
            {
                case "add":
                    Menu entity = new Menu();
                    entity.PId = Pid;
                    entity.Name = Name;
                    entity.Link = Link;
                    entity.ModuleId = 1;
                    entity.Created_Date = DateTime.Now;
                    entity.flag = flag;
                    if (Repository.Insert(entity))
                        return Json("ok", JsonRequestBehavior.AllowGet);
                    return Json("err", JsonRequestBehavior.AllowGet);
                case "del":
                    bool _flag = true;
                    foreach (var item in ids)
                    {
                        int Id = ConvertUtility.ToInt(item);
                        Menu _entity = Repository.GetOne(Id);
                        if (_delForever)
                            Repository.Delete(_entity);
                        else
                        {
                            _entity.flag = "D";
                            if (!Repository.Update(_entity))
                                _flag = false;
                        }
                    }
                    if (_flag)
                        return Json("ok", JsonRequestBehavior.AllowGet);
                    return Json("err", JsonRequestBehavior.AllowGet);
                case "edit":
                    Menu myfentity = Repository.GetOne(ConvertUtility.ToInt(ids[0]));
                    myfentity.PId = Pid;
                    myfentity.Name = Name;
                    myfentity.Link = Link;
                    myfentity.ModuleId = 1;
                    myfentity.flag = flag;
                    if (Repository.Update(myfentity))
                        return Json("ok", JsonRequestBehavior.AllowGet);
                    return Json("err", JsonRequestBehavior.AllowGet);
                default:
                    return Json("err", JsonRequestBehavior.AllowGet);
            }

        }
    }
}
